<template>
	<view class="page">

		<!-- 订单tab -->
		<view class="order-tab">
			<view class="tab" :class="{'action':OrderType==0}" @click="onOrderTab(0)">
				<text>全部</text>
				<text class="line"></text>
			</view>
			
			<view class="tab" :class="{'action':OrderType==1}" @click="onOrderTab(1)">
				<text>在售中({{Onsale}})</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==2}" @click="onOrderTab(2)">
				<text>审核中({{Underreview}})</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==3}" @click="onOrderTab(3)">
				<text>驳回({{Plisting}})</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==4}" @click="onOrderTab(4)">
				<text>锁单({{suodan}})</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':OrderType==5}" @click="onOrderTab(5)">
				<text>已完成({{end}})</text>
				<text class="line"></text>
			</view>
		</view>

		<view class="head">

			<view class="title" @click="isReturn=true">商品分类></view>
			<u-input v-model="model1.userInfo.return['name']" disabled border="none"></u-input>
			<u-picker :keyName="'name'" :defaultIndex="1" :show="isReturn" :columns="shopLists" @close="close1" @confirm="confirm1"></u-picker>

			<view class="edit" @click="isEdit = !isEdit">
				<text>批量</text>
			</view>
		</view>
		<!-- 购物车列表 -->
		<mescroll-body ref="mescrollRef" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption"
			:top="0">
			<view class="cart-list">
				<view class="list" v-for="(item, index) in goodsList" @click="onSkip('goods')" :key="index">
					<view class="check" v-if="isEdit" @click="toggleCheck(index)">
						<text class="iconfont" :class="item.isChecked ? 'icon-checked' : 'icon-check'"></text>
					</view>
					<view class="goods">
						<view class="thumb">
							<image :src="item.img_list[0]" mode=""></image>
						</view>
						<view class="item">
							<view class="title">
								<text class="two-omit">{{ item.title }}</text>
							</view>

							<view class="price-num">
								<view class="price">

									<text class="max">售价：{{ item.real_price }}</text>
									<text class="min">元</text>
								</view>
								<view class="edit" v-if="item.status == 1 ||item.status == 3" @click="table(item.id)">
									编辑
								</view>
							
								<view class="edit"  v-if="item.status == 1 " @click="savePaySort(item.id)">
									靠前
								</view>
							</view>
							<view class="title">
								<text class="two-omit">{{ item.reject_msg||'' }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<button @click="gotoyihuoadd">添加</button>
		</mescroll-body>
		<!-- 属性规格 -->
		<goods-attr ref="GoodsAttr"></goods-attr>
		
	
	
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import {
		getStoreGoodsListYihuo,
		saveStoreGoods,
		getCategoryList,
		getLSpaySnSortgoods,
		delNameById,
		getLSpaySort
	} from '@/config/api.js';
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				Onsale: 0,
				Plisting: 0,
				suodan: 0,
				end: 0,
				Underreview: 0,
				isAdd: false,
				OrderType: 0,
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				isReturn: false,
				upOption: {
					use: false,
					toTop: {
						src: '',
					}
				},
				listQuery:{
					page:1,
					limit:30,
					nav_id: uni.getStorageSync('pathNavId'),
					store_id: uni.getStorageSync('pathStoreId'),
					status: undefined,
					category_id: undefined
				},
				shopLists: [],
				show: false,
				model1: {
					userInfo: {
						return: [{name: '全部'}],
					},
				},
				isEdit: false,
				goodsList: [],
				showDetailItem: null,

			}
		},
		mounted() {
			uni.hideTabBar()
			getCategoryList().then(res => {
				
				this.shopLists = [[{name: '全部',id: undefined},...res]];
			})
			this.getListGoods();
		},
		computed: {
			allSelected() {
				return this.goodsList.every(item => item.isChecked);
			},
			selectedItemCount() {
				return this.goodsList.filter(item => item.isChecked).length;
			},

		},
		
		methods: {
			gotoyihuoadd(){
				uni.setStorageSync('goodsId','')
				uni.navigateTo({
					url: '/moduleD/hongShanHun/addShop/yihuoadd',
				})
			},
			savePaySort(storeid){
				const _this = this;
				uni.showModal({
					title: '确认付费提升排名？',
					success: function(res) {
						if (res.confirm) {
							getLSpaySort().then(data => {
								if(data.signType){
									wx.requestPayment({
									  timeStamp: data.timeStamp,
									  nonceStr: data.nonceStr,
									  package: data.package,
									  signType: data.signType,
									  paySign: data.paySign,
									  success (res) { 
										  getLSpaySnSortgoods({
											  id: storeid,
											  isyihuo:1,
										  }).then(res => {
											  console.log(res,'=============')
											  uni.$u.toast('操作成功!')
										  })
									  },
									  fail (res) { 
										  // 支付失败 删除商品
										  delNameById({order_sn: data._order_sn})
									  }
									})
								}
							})
						}
					}
				});
			},
			getListGoods(){
				getStoreGoodsListYihuo(this.listQuery).then(res => {
					this.goodsList = res.data;
					this.Onsale = res.Onsale;
					this.Plisting = res.Plisting;
					this.suodan = res.suodan;
					this.end = res.end;
					this.Underreview = res.Underreview;
					console.log(res,'==============================')
				})
			},
			toggleAdd(data) {
			
				saveStoreGoods({
					id: data.id,
					isAdd: data.isAdd?0:1
				}).then(res=>{
					this.getListGoods();
				})
			},
			//选择商品分类
			close1() {
				this.isReturn = false; // 关闭选择器
			},
			confirm1(selectedItems) {
				// selectedItems 是选中的商品类目
				
				this.model1.userInfo.return = selectedItems.value[0]; // 更新输入框的值为选中的商品类目
				console.log('哈哈哈哈', selectedItems.value[0]);
				this.listQuery.category_id = selectedItems.value[0]['id'];
				
				this.isReturn = false; // 关闭选择器
				this.getListGoods();
			},
			table(id) {
				
				uni.setStorageSync('goodsId',id)
				uni.navigateTo({
					url: '/moduleD/hongShanHun/addShop/yihuoadd',
				})
			},
			onOrderTab(status,type) {
				this.OrderType = status;
				this.listQuery['status'] = status;
				this.getListGoods();
				// // #ifdef H5
				// uni.redirectTo({
				// 	url: '/pages/MyOrderList/MyOrderList?type=' + type,
				// })
				// //#endif

			},

			/*下拉刷新的回调, 有三种处理方式:*/
			downCallback() {
				this.mescroll.endSuccess();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				setTimeout(() => {
					this.mescroll.endByPage(10, 20);
				}, 2000)
			},

			// 选择sku

			open() {
				// console.log('open');
			},
			close() {
				this.show = false
				// console.log('close');
			},
			toggleCheck(index) {
				this.goodsList[index].isChecked = !this.goodsList[index].isChecked;
			},
			toggleSelectAll() {
				const newState = !this.allSelected;
				this.goodsList.forEach(item => {
					item.isChecked = newState;
				});
			},

			deleteSelectedItems() {
				this.goodsList = this.goodsList.filter(item => !item.isChecked); // 删除所有选中的商品
			},

			/**
			 * 跳转点击
			 * @param {String} type 跳转类型
			 */
			onSkip(type) {
				switch (type) {
					case 'classify':
						uni.navigateTo({
							url: '/pages/SearchGoodsList/SearchGoodsList',
						})
						break;
					case 'goods':
						uni.navigateTo({
							url: '/pages/GoodsDetails/GoodsDetails',
							animationType: 'zoom-fade-out',
							animationDuration: 200
						})
						break;
				}
			}
		},
	}
</script>

<style scoped lang="scss">
	@import 'shangjia.scss';
</style>